<?php

class MenuInstall
{
	
	private $_config = array(
		'name' => 'menu',
		'title' => 'Menu module',
		'dependencies' => array(
			'languages' => array(
				'{{menu_items}}' => array(
					'fields' => array (
						'title' => 'varchar(32) NOT NULL',
					)
				),
			),
		),
		'import' => array(
			'application.modules.menu.MenuModule',
			'application.modules.menu.models.*',
		)
	);

	public function config()
	{
		return $this->_config;
	}
	
	public function install()
	{
		/* install menus */
		$sql = "DROP TABLE IF EXISTS {{menu}};";
		Yii::app()->db->createCommand($sql)->execute();

		$sql="
	CREATE TABLE IF NOT EXISTS {{menu}} (
	  `id` int(3) NOT NULL AUTO_INCREMENT,
	  `label` varchar(50) NOT NULL,
	  `title` varchar(255) NOT NULL,
	  PRIMARY KEY (`id`),
	  UNIQUE KEY `label` (`label`)
	) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;";
		Yii::app()->db->createCommand($sql)->execute();

		$sql="INSERT INTO {{menu}} (label, title) VALUES (:label, :title);";
		$command = Yii::app()->db->createCommand($sql);

		$command->bindValue(":label", "admin_menu", PDO::PARAM_STR);
		$command->bindValue(":title", "Меню администратора", PDO::PARAM_STR);
		$command->execute();

		$command->bindValue(":label", "user_guest_menu", PDO::PARAM_STR);
		$command->bindValue(":title", "Меню гостя", PDO::PARAM_STR);
		$command->execute();

		/* install menu items */
		$languages = Languages::model()->getLanguages();	

		foreach ($languages as $language)
		{
			foreach ($this->_config['dependencies']['languages']['{{menu_items}}']['fields'] as $field => $type)
			{
				$keys[] = $field.'_'.$language['code'];
				$values[] = ':'.$field.'_'.$language['code'];
				$fields[] = "`".$field."_".$language['code']."` ".$type.",";
			}
		}

		$sql = "DROP TABLE IF EXISTS {{menu_items}};";
		Yii::app()->db->createCommand($sql)->execute();

		$sql="
	CREATE TABLE IF NOT EXISTS {{menu_items}} (
	  `id` int(11) NOT NULL AUTO_INCREMENT,
	  `menu_id` int(11) NOT NULL,";
		$sql .= implode("", $fields);
		$sql .= "
	  `route` varchar(255) NOT NULL,
	  `parent` int(11) DEFAULT '0',
	  `sorter` int(11) NOT NULL DEFAULT '0',
	  `active` tinyint(1) NOT NULL DEFAULT '1',
	  PRIMARY KEY (`id`)
	) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;";
		Yii::app()->db->createCommand($sql)->execute();

		$sql="INSERT INTO {{menu_items}} (menu_id, ".implode(', ', $keys).", route, parent, sorter, active) VALUES (:menu_id, ".implode(', ', $values).", :route, :parent, :sorter, :active);";
		$command = Yii::app()->db->createCommand($sql);

		$command->bindValue(":menu_id", "1", PDO::PARAM_INT);
		foreach ($languages as $language)
		{
			$command->bindValue(":title_".$language['code'], Yii::t('MenuModule.install', 'Dashboard', array(), null, $language['code']), PDO::PARAM_STR);
		}
		$command->bindValue(":route", "site/index", PDO::PARAM_STR);
		$command->bindValue(":parent", "0", PDO::PARAM_INT);
		$command->bindValue(":sorter", "1", PDO::PARAM_INT);
		$command->bindValue(":active", "1", PDO::PARAM_INT);
		$command->execute();

		$command->bindValue(":menu_id", "1", PDO::PARAM_INT);
		foreach ($languages as $language)
		{
			$command->bindValue(":title_".$language['code'], Yii::t('MenuModule.install', 'Users', array(), null, $language['code']), PDO::PARAM_STR);
		}
		$command->bindValue(":route", "user/admin/default/index", PDO::PARAM_STR);
		$command->bindValue(":parent", "0", PDO::PARAM_INT);
		$command->bindValue(":sorter", "2", PDO::PARAM_INT);
		$command->bindValue(":active", "1", PDO::PARAM_INT);
		$command->execute();

		$command->bindValue(":menu_id", "1", PDO::PARAM_INT);
		foreach ($languages as $language)
		{
			$command->bindValue(":title_".$language['code'], Yii::t('MenuModule.install', 'Infopages', array(), null, $language['code']), PDO::PARAM_STR);
		}
		$command->bindValue(":route", "infopages/admin/default/index", PDO::PARAM_STR);
		$command->bindValue(":parent", "0", PDO::PARAM_INT);
		$command->bindValue(":sorter", "3", PDO::PARAM_INT);
		$command->bindValue(":active", "1", PDO::PARAM_INT);
		$command->execute();

		$command->bindValue(":menu_id", "1", PDO::PARAM_INT);
		foreach ($languages as $language)
		{
			$command->bindValue(":title_".$language['code'], Yii::t('MenuModule.install', 'News', array(), null, $language['code']), PDO::PARAM_STR);
		}
		$command->bindValue(":route", "news/admin/default/index", PDO::PARAM_STR);
		$command->bindValue(":parent", "0", PDO::PARAM_INT);
		$command->bindValue(":sorter", "4", PDO::PARAM_INT);
		$command->bindValue(":active", "1", PDO::PARAM_INT);
		$command->execute();

		$command->bindValue(":menu_id", "1", PDO::PARAM_INT);
		foreach ($languages as $language)
		{
			$command->bindValue(":title_".$language['code'], Yii::t('MenuModule.install', 'FAQ', array(), null, $language['code']), PDO::PARAM_STR);
		}
		$command->bindValue(":route", "faq/admin/default/index", PDO::PARAM_STR);
		$command->bindValue(":parent", "0", PDO::PARAM_INT);
		$command->bindValue(":sorter", "5", PDO::PARAM_INT);
		$command->bindValue(":active", "1", PDO::PARAM_INT);
		$command->execute();

		$command->bindValue(":menu_id", "1", PDO::PARAM_INT);
		foreach ($languages as $language)
		{
			$command->bindValue(":title_".$language['code'], Yii::t('MenuModule.install', 'Languages', array(), null, $language['code']), PDO::PARAM_STR);
		}
		$command->bindValue(":route", "languages/admin/default/index", PDO::PARAM_STR);
		$command->bindValue(":parent", "8", PDO::PARAM_INT);
		$command->bindValue(":sorter", "6", PDO::PARAM_INT);
		$command->bindValue(":active", "1", PDO::PARAM_INT);
		$command->execute();

		$command->bindValue(":menu_id", "1", PDO::PARAM_INT);
		foreach ($languages as $language)
		{
			$command->bindValue(":title_".$language['code'], Yii::t('MenuModule.install', 'Menu', array(), null, $language['code']), PDO::PARAM_STR);
		}
		$command->bindValue(":route", "menu/admin/default/index", PDO::PARAM_STR);
		$command->bindValue(":parent", "8", PDO::PARAM_INT);
		$command->bindValue(":sorter", "7", PDO::PARAM_INT);
		$command->bindValue(":active", "1", PDO::PARAM_INT);
		$command->execute();

		$command->bindValue(":menu_id", "1", PDO::PARAM_INT);
		foreach ($languages as $language)
		{
			$command->bindValue(":title_".$language['code'], Yii::t('MenuModule.install', 'Settings', array(), null, $language['code']), PDO::PARAM_STR);
		}
		$command->bindValue(":route", "", PDO::PARAM_STR);
		$command->bindValue(":parent", "0", PDO::PARAM_INT);
		$command->bindValue(":sorter", "8", PDO::PARAM_INT);
		$command->bindValue(":active", "1", PDO::PARAM_INT);
		$command->execute();

		$command->bindValue(":menu_id", "1", PDO::PARAM_INT);
		foreach ($languages as $language)
		{
			$command->bindValue(":title_".$language['code'], Yii::t('MenuModule.install', 'Site Settings', array(), null, $language['code']), PDO::PARAM_STR);
		}
		$command->bindValue(":route", "settings/admin/default/index", PDO::PARAM_STR);
		$command->bindValue(":parent", "8", PDO::PARAM_INT);
		$command->bindValue(":sorter", "2", PDO::PARAM_INT);
		$command->bindValue(":active", "1", PDO::PARAM_INT);
		$command->execute();

		$command->bindValue(":menu_id", "1", PDO::PARAM_INT);
		foreach ($languages as $language)
		{
			$command->bindValue(":title_".$language['code'], Yii::t('MenuModule.install', 'SEO Settings', array(), null, $language['code']), PDO::PARAM_STR);
		}
		$command->bindValue(":route", "seo/admin/default/index", PDO::PARAM_STR);
		$command->bindValue(":parent", "8", PDO::PARAM_INT);
		$command->bindValue(":sorter", "2", PDO::PARAM_INT);
		$command->bindValue(":active", "1", PDO::PARAM_INT);
		$command->execute();

		$command->bindValue(":menu_id", "2", PDO::PARAM_INT);
		foreach ($languages as $language)
		{
			$command->bindValue(":title_".$language['code'], Yii::t('MenuModule.install', 'Home', array(), null, $language['code']), PDO::PARAM_STR);
		}
		$command->bindValue(":route", "/site/index", PDO::PARAM_STR);
		$command->bindValue(":parent", "0", PDO::PARAM_INT);
		$command->bindValue(":sorter", "1", PDO::PARAM_INT);
		$command->bindValue(":active", "1", PDO::PARAM_INT);
		$command->execute();

		$command->bindValue(":menu_id", "2", PDO::PARAM_INT);
		foreach ($languages as $language)
		{
			$command->bindValue(":title_".$language['code'], Yii::t('MenuModule.install', 'Contacts', array(), null, $language['code']), PDO::PARAM_STR);
		}
		$command->bindValue(":route", "contact/default/index", PDO::PARAM_STR);
		$command->bindValue(":parent", "0", PDO::PARAM_INT);
		$command->bindValue(":sorter", "2", PDO::PARAM_INT);
		$command->bindValue(":active", "1", PDO::PARAM_INT);
		$command->execute();

		$command->bindValue(":menu_id", "2", PDO::PARAM_INT);
		foreach ($languages as $language)
		{
			$command->bindValue(":title_".$language['code'], Yii::t('MenuModule.install', 'Register', array(), null, $language['code']), PDO::PARAM_STR);
		}
		$command->bindValue(":route", "/user/default/register", PDO::PARAM_STR);
		$command->bindValue(":parent", "0", PDO::PARAM_INT);
		$command->bindValue(":sorter", "3", PDO::PARAM_INT);
		$command->bindValue(":active", "1", PDO::PARAM_INT);
		$command->execute();

		$command->bindValue(":menu_id", "2", PDO::PARAM_INT);
		foreach ($languages as $language)
		{
			$command->bindValue(":title_".$language['code'], Yii::t('MenuModule.install', 'News', array(), null, $language['code']), PDO::PARAM_STR);
		}
		$command->bindValue(":route", "/news/default/index", PDO::PARAM_STR);
		$command->bindValue(":parent", "0", PDO::PARAM_INT);
		$command->bindValue(":sorter", "4", PDO::PARAM_INT);
		$command->bindValue(":active", "1", PDO::PARAM_INT);
		$command->execute();

		$command->bindValue(":menu_id", "2", PDO::PARAM_INT);
		foreach ($languages as $language)
		{
			$command->bindValue(":title_".$language['code'], Yii::t('MenuModule.install', 'FAQ', array(), null, $language['code']), PDO::PARAM_STR);
		}
		$command->bindValue(":route", "/faq/default/index", PDO::PARAM_STR);
		$command->bindValue(":parent", "0", PDO::PARAM_INT);
		$command->bindValue(":sorter", "5", PDO::PARAM_INT);
		$command->bindValue(":active", "1", PDO::PARAM_INT);
		$command->execute();

		$command->bindValue(":menu_id", "2", PDO::PARAM_INT);
		foreach ($languages as $language)
		{
			$command->bindValue(":title_".$language['code'], Yii::t('MenuModule.install', 'About', array(), null, $language['code']), PDO::PARAM_STR);
		}
		$command->bindValue(":route", "infopages/default/view?id=1", PDO::PARAM_STR);
		$command->bindValue(":parent", "0", PDO::PARAM_INT);
		$command->bindValue(":sorter", "6", PDO::PARAM_INT);
		$command->bindValue(":active", "1", PDO::PARAM_INT);
		$command->execute();

		$command->bindValue(":menu_id", "2", PDO::PARAM_INT);
		foreach ($languages as $language)
		{
			$command->bindValue(":title_".$language['code'], Yii::t('MenuModule.install', 'Login', array(), null, $language['code']), PDO::PARAM_STR);
		}
		$command->bindValue(":route", "/site/login", PDO::PARAM_STR);
		$command->bindValue(":parent", "0", PDO::PARAM_INT);
		$command->bindValue(":sorter", "7", PDO::PARAM_INT);
		$command->bindValue(":active", "1", PDO::PARAM_INT);
		$command->execute();

	}

	public function uninstall()
	{
		Yii::app()->db->createCommand()->dropTable('{{menu}}');
		Yii::app()->db->createCommand()->dropTable('{{menu_items}}');
	}
}